﻿//------------------------------------------------------------------------------
//Author: Christoph van der Fecht
//CodePlex: http://www.codeplex.com/site/users/view/ChristophvdF
//
//Copyright(c) Christoph van der Fecht
//------------------------------------------------------------------------------
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using SerienNewsReader.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System;
using System.Linq;

namespace SerienNewsReader.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class MoviesViewModel : ViewModelBase
    {
        /// <summary>
        /// List of all movies
        /// </summary>
        private List<RSSFeed> allMovies = default(List<RSSFeed>);

        /// <summary>
        /// Curently displayed movies
        /// </summary>
        private ObservableCollection<RSSFeed> movies = default(ObservableCollection<RSSFeed>);


        /// <summary>
        /// Initializes a new instance of the MoviesViewModel class.
        /// </summary>
        public MoviesViewModel()
        {
            Messenger.Default.Register<string>(this, this.ApplyMovieTitleFilter);
            if (IsInDesignMode)
            {
                List<RSSFeed> dummyMovies = new List<RSSFeed>();
                for (int movieCounter = 0; movieCounter < 50; movieCounter++)
                {
                    dummyMovies.Add(new RSSFeed(string.Format("Test Movie {0}", movieCounter),
                                                "TestLanguage", new Uri("http://www.test.cc")));
                }
                this.Movies = new ObservableCollection<RSSFeed>(dummyMovies);
            }
            else
            {
                SerienNewsReaderMVVM.Loader.Instance.DownloadCompleted += new SerienNewsReaderMVVM.Loader.DownloadCompletedDelegate(OnDownloadCompelted);
                SerienNewsReaderMVVM.Loader.Instance.LoadSeries();
            }
        }

        void OnDownloadCompelted(object sender, SerienNewsReaderMVVM.EventArguments.DownloadFinishedEventArgs e)
        {
            SerienNewsReaderMVVM.Loader.Instance.DownloadCompleted -= this.OnDownloadCompelted;
            if (e.TvMovies != null)
            {
                this.allMovies = e.TvMovies;
                this.Movies = new ObservableCollection<RSSFeed>(e.TvMovies);
            }
        }

        /// <summary>
        /// Applies the filter on the movie list
        /// </summary>
        /// <param name="filter">Criteria for filtering</param>
        private void ApplyMovieTitleFilter(string filter)
        {
            if (string.IsNullOrEmpty(filter))
            {
                return;
            }
            else
            {
                if (filter.Contains('.'))
                {
                    filter = filter.Replace('.', ' ');
                }

                this.Movies = new ObservableCollection<RSSFeed>(
                    this.allMovies.Where(x => x.Title.ToLower().Contains(filter.ToLower())));
            }
        }

        /// <summary>
        /// Gets or sets the movies to display
        /// </summary>
        public ObservableCollection<RSSFeed> Movies
        {
            get
            {
                return this.movies;
            }
            set
            {
                if (value != this.movies)
                {
                    this.movies = new ObservableCollection<RSSFeed>(value.OrderBy(x => x.Title));
                    base.RaisePropertyChanged("Movies");
                }
            }
        }

        /// <summary>
        /// Perfomres the cleanup
        /// </summary>
        public override void Cleanup()
        {
            this.allMovies = null;
            this.movies = null;
            Messenger.Default.Unregister<string>(this, this.ApplyMovieTitleFilter);
            base.Cleanup();
        }
    }
}